package com.gzdh.tang.mobile.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gzdh.tang.comm.vo.Result;
import com.gzdh.tang.mobile.entity.Bmcm;
import com.gzdh.tang.mobile.entity.Kl;
import com.gzdh.tang.mobile.service.BmcmService;
import com.gzdh.tang.mobile.service.KlService;
import com.gzdh.tang.modu.entity.MacMainpSbruan;
import com.gzdh.tang.modu.entity.MacMainpSbruanDinner;
import com.gzdh.tang.modu.entity.MacMainpSbruanKl;
import com.gzdh.tang.modu.service.MacMainpSbruanDinnerService;
import com.gzdh.tang.modu.service.MacMainpSbruanKlService;
import com.gzdh.tang.modu.service.MacMainpSbruanService;
import com.gzdh.tang.modu.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("api/mobile/machine/")
@RestController
public class MachineDeviceController {


    @Autowired
    MacMainpSbruanService macMainpSbruanService;

    @Autowired
    MacMainpSbruanDinnerService macMainpSbruanDinnerService;

    @Autowired
    MacMainpSbruanKlService macMainpSbruanKlService;

    @Autowired
    KlService klService;

    @Autowired
    BmcmService bmcmService;

    /**
     * 根据SN获取卡类
     *
     * @param deviceSn
     * @return
     */
    @PostMapping("findSbruan")
    public Result<Object> findSbruanKl(@RequestParam("deviceSn") String deviceSn,
                                       @RequestParam("urlTime") String urlTime,
                                       @RequestParam("sign") String sign) {
        Result<Object> result = new Result<>();
        System.out.println("SS" + sign);
        MacMainpSbruan macMainpSbruan = macMainpSbruanService.getOne(new QueryWrapper<MacMainpSbruan>().lambda().eq(MacMainpSbruan::getMac, deviceSn));
        List<MacMainpSbruanKl> macMainpSbruanKlList = macMainpSbruanKlService.list(new QueryWrapper<MacMainpSbruanKl>().lambda().eq(MacMainpSbruanKl::getDeviceId, macMainpSbruan.getId()));
        List<MacMainpSbruanDinner> macMainpSbruanDinnerList = macMainpSbruanDinnerService.list(new QueryWrapper<MacMainpSbruanDinner>().lambda().eq(MacMainpSbruanDinner::getDeviceId, macMainpSbruan.getId()));
        List<Kl> klList = klService.list();
        List<Bmcm> bmcmList = bmcmService.list();
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("sbruan", macMainpSbruan);
        resMap.put("sbruanKl", macMainpSbruanKlList);
        resMap.put("sbruanDinner", macMainpSbruanDinnerList);
        resMap.put("kl", klList);
        resMap.put("bmcm", bmcmList);
        result.setResult(resMap);
        return result;
    }

    /**
     * 获取全部卡类
     *
     * @return
     */
    @PostMapping("findKlAll")
    public Result<Object> findKlAll() {
        Result<Object> result = new Result<>();
        List<Kl> klList = klService.list();
        result.setResult(klList);
        return result;
    }

    /**
     * 获取全部餐次
     *
     * @return
     */
    @PostMapping("findDinnerAll")
    public Result<Object> findDinnerAll() {
        Result<Object> result = new Result<>();
        result.setResult(bmcmService.list());
        return result;
    }

    @PostMapping("deleteDinner")
    public Result<Object> deleteDinner(@RequestParam("deviceId") String deviceId, @RequestParam("dinnerId") String dinnerId) {
        Result<Object> result = new Result<>();
        macMainpSbruanDinnerService.remove(new QueryWrapper<MacMainpSbruanDinner>().lambda()
                .eq(MacMainpSbruanDinner::getDinner, dinnerId).eq(MacMainpSbruanDinner::getDeviceId, deviceId));
        return result;
    }

    @PostMapping("deleteKl")
    public Result<Object> deleteKl(@RequestParam("deviceId") String deviceId, @RequestParam("klId") String klId) {
        Result<Object> result = new Result<>();
        macMainpSbruanKlService.remove(new QueryWrapper<MacMainpSbruanKl>().lambda()
                .eq(MacMainpSbruanKl::getDeviceId, deviceId)
                .eq(MacMainpSbruanKl::getKl, klId));
        return result;
    }

    @PostMapping("addKl")
    public Result<Object> addKl(@RequestParam("deviceId") Long deviceId, @RequestParam("klId") String klId) {
        Result<Object> result = new Result<>();
        int count = macMainpSbruanKlService.count(new QueryWrapper<MacMainpSbruanKl>()
                .lambda().eq(MacMainpSbruanKl::getDeviceId, deviceId)
                .eq(MacMainpSbruanKl::getKl, klId));
        if (count > 0) {
            return Result.error("该卡类已存在，添加失败");
        }
        MacMainpSbruanKl macMainpSbruanKl = new MacMainpSbruanKl();
        macMainpSbruanKl.setDeviceId(deviceId);
        macMainpSbruanKl.setZk("1");
        macMainpSbruanKl.setKl(klId);
        macMainpSbruanKlService.save(macMainpSbruanKl);
        return result;
    }

    @PostMapping("addDinner")
    public Result<Object> addDinner(@RequestParam("deviceId") Long deviceId, @RequestParam("dinnerId") String dinnerId,
                                    @RequestParam("startTime") String startTime, @RequestParam("endTime") String endTime) {
        Result<Object> result = new Result<>();
        List<MacMainpSbruanDinner> dinnerList = macMainpSbruanDinnerService.list(new QueryWrapper<MacMainpSbruanDinner>().lambda()
                .eq(MacMainpSbruanDinner::getDeviceId, deviceId));
        try {
            for (MacMainpSbruanDinner goo : dinnerList) {
                if (DateUtil.hourMinuteBetween(startTime, goo.getStartTime(), goo.getEndTime())) {
                    return Result.error("时间段重复,添加失败");
                }
                if (DateUtil.hourMinuteBetween(endTime, goo.getStartTime(), goo.getEndTime())) {
                    return Result.error("时间段重复,添加失败");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("时间段错误,添加失败");
        }
        MacMainpSbruanDinner macMainpSbruanDinner = new MacMainpSbruanDinner();
        macMainpSbruanDinner.setDinner(dinnerId);
        macMainpSbruanDinner.setStartTime(startTime);
        macMainpSbruanDinner.setEndTime(endTime);
        macMainpSbruanDinner.setDeviceId(deviceId);
        macMainpSbruanDinnerService.save(macMainpSbruanDinner);
        return result;
    }

    @PostMapping("updateSbruan")
    public Result<Object> updateSbruan(@RequestParam("deviceId") String deviceId,
                                       @RequestParam("posnum") String posnum,
                                       @RequestParam("code") String code) {
        Result<Object> result = new Result<>();
        macMainpSbruanService.lambdaUpdate().eq(MacMainpSbruan::getId, deviceId)
                .set(MacMainpSbruan::getPos, posnum)
                .set(MacMainpSbruan::getCode, code).update();
        return result;
    }
}
